//
//  callSettingViewController.m
//  SIPphone
//
//  Created by wrt on 13-11-14.
//  Copyright (c) 2013年 wrtsoft. All rights reserved.
//

#import "callSettingViewController.h"

@interface callSettingViewController ()

@end

@implementation callSettingViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = NSLocalizedString(@"call Settings", @"呼叫配置 Call Settings");
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.segment = [[HMSegmentedControl alloc] initWithSectionTitles:@[@"183",@"180"]];
    [self.segment setFrame:CGRectMake(195, 20, 105, 30)];
    [self.segment setSelectionIndicatorHeight:3.5f];
    [self.segment setBackgroundColor:[UIColor whiteColor]];
    [self.segment setTextColor:[UIColor colorWithRed:83/255.0 green:215/255.0 blue:105/255.0 alpha:1.0]];
    [self.segment setSelectionIndicatorColor:[UIColor colorWithRed:83/255.0 green:215/255.0 blue:105/255.0 alpha:1.0]];
    [self.segment setSelectionIndicatorMode:HMSelectionIndicatorFillsSegment];
    [self.segment addTarget:self action:@selector(segamentSelectChanged:) forControlEvents:UIControlEventValueChanged];
    [self.segment setSegmentEdgeInset:UIEdgeInsetsMake(0, 6, 0, 6)];
    [self.segment setFont:[UIFont systemFontOfSize:17.0f]];
    [self.view addSubview:self.segment];
    // Do any additional setup after loading the view from its nib.
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    //自动接听
    NSString *isOpenAutoAnswer = [[NSString alloc] init];
    isOpenAutoAnswer = [defaults objectForKey:@"SAVEISOPENAUTOANSWER"];
    if (isOpenAutoAnswer.length > 0)
    {
        if ([isOpenAutoAnswer isEqualToString:@"YES"])
        {
            [self.autoAnswerSwitch setOn:YES];
        }
        else if ([isOpenAutoAnswer isEqualToString:@"NO"])
        {
            [self.autoAnswerSwitch setOn:NO];
        }
    }
    
    NSString *tempSegmented = [[NSString alloc] init];
    tempSegmented = [defaults objectForKey:@"SAVEAUTOANSWERCALLNUMBER"];
    if (tempSegmented.length > 0)
    {
        if ([tempSegmented isEqualToString:@"183"])
        {
            [self.segment setSelectedIndex:0];
        }
        else if ([tempSegmented isEqualToString:@"180"])
        {
            [self.segment setSelectedIndex:1];
        }
    }
}

- (IBAction)openAutoAnswerSwitch:(id)sender
{
    UISwitch *switchControl = (UISwitch *)sender;
    SIPInterface *interface = [SIPInterface shareSIPInterface];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    BOOL isAutoAnswer = switchControl.on;
    if (isAutoAnswer == YES)
    {
        [interface isOpenAutoAnswer:YES];
        [defaults setObject:@"YES" forKey:@"SAVEISOPENAUTOANSWER"];
    }
    else
    {
        [interface isOpenAutoAnswer:NO];
        [defaults setObject:@"NO" forKey:@"SAVEISOPENAUTOANSWER"];
    }
    [defaults synchronize];
}

- (IBAction)segamentSelectChanged:(HMSegmentedControl *)segmentedControl
{
    SIPInterface *interface = [SIPInterface shareSIPInterface];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if (segmentedControl.selectedIndex == 0)
    {
        [interface autoAnswerCallNumber:@"183"];
        [defaults setObject:@"183" forKey:@"SAVEAUTOANSWERCALLNUMBER"];
    }
    else if (segmentedControl.selectedIndex == 1)
    {
        [interface autoAnswerCallNumber:@"180"];
        [defaults setObject:@"180" forKey:@"SAVEAUTOANSWERCALLNUMBER"];
    }
    [defaults synchronize];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
